﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Runtime.InteropServices;
using System.Text;

namespace SetSailProcess
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            //{D07A13D1-FDFE-49FB-ACC7-28F705E15E2E}
            bool flag = CheckIfAppInstall("SetSailSetUp");
            MessageBox.Show("是否安装过：" + flag);
            if (flag)
            {
                string sysroot = System.Environment.SystemDirectory;
                Process prc = Process.Start(sysroot + "\\msiexec.exe ", "/x {D07A13D1-FDFE-49FB-ACC7-28F705E15E2E} /qn");
                prc.WaitForExit();
                MessageBox.Show("卸载成功");
            }
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }

        /// <summary>
        /// 查询当前系统是否已经安装过软件
        /// </summary>
        /// <param name="appKeyName"></param>
        /// <returns></returns>
        public static bool CheckIfAppInstall(string appKeyName)
        {
            bool result = false;
            try
            {
                Microsoft.Win32.RegistryKey uninstallNode = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall");
                foreach (string subKeyName in uninstallNode.GetSubKeyNames())
                {
                    Microsoft.Win32.RegistryKey subKey = uninstallNode.OpenSubKey(subKeyName);
                    object displayName = subKey.GetValue("DisplayName");
                    if (displayName != null)
                    {
                        if (displayName.ToString().Contains(appKeyName))
                        {
                            result = true;
                            break;
                        }

                    }
                }
            }
            catch (Exception e)
            {
                string error = $"error in CheckIfAppInstall：{e.Message}";
                throw new Exception(error);
            }
            return result;
        }
    }
}
